﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 头像抽奖工具
{
    public static class UIUtils
    {
        public delegate void InvokeTextDelegate(Control control, string text);

        public delegate void InvokeImageDelegate(Panel control, string path);

        public static void InvokeText(this Control control, string text)
        {
            try
            {
                if (control.InvokeRequired)
                {
                    control.BeginInvoke(new InvokeTextDelegate(InvokeText), new object[] { control, text });
                }
                else
                {

                    if (control is TextBox)
                    {
                        ((TextBox)control).Text = text;
                    }
                    else if (control is Label)
                    {
                        ((Label)control).Text = text;
                    }
                    else if (control is Button)
                    {
                        ((Button)control).Text = text;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void InvokeImage(this Panel control, string path)
        {
            try
            {
                if (control.InvokeRequired)
                {
                    control.BeginInvoke(new InvokeImageDelegate(InvokeText), new object[] { control, path });
                }
                else
                {
                    control.BackgroundImage = new Bitmap(path);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
